﻿var x = 0;//top坐标
var y = 0;//left坐标
var i = 0;//选中行数
var sum = 0;//总行数
var t = null;//临时变量
var dt = null;//数据表格
//写入层
// server:SQL服务器地址,database:数据库名称,uid:用户名,pwd:密码,tablename:表名,colname:列名,control:应用到控件名
function showTiShi(tableName,colName,colName2,control)
{  
    getXY(document.getElementById(control));
    var innerHtml=""; 
    var vsKey = document.getElementById(control).value;
    if(vsKey==null||vsKey=="")
    {   
        closeTiShi();
    }
    if(vsKey!=null&&vsKey!=""&&event.keyCode!=37&&event.keyCode!=38&&event.keyCode!=39&&event.keyCode!=40)
    {
        dt= InputHelp.GetTxt_TiShi(tableName,colName,colName2,vsKey).value;
        if(dt!=null&&dt.Rows.length>0)
        {
            sum = dt.Rows.length;
            innerHtml = "<table style=\"font-size:10pt;width:153px;\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\r\n";
            for(var i=0;i<dt.Rows.length;i++)
            {
                innerHtml +="<tr id=\""+i+"\" onmousedown=\"getCheck('"+dt.Rows[i][colName]+"','"+control+"')\" onmouseup=\"closeTiShi()\" onmouseover=\"mouseOver(this.id)\" onmouseout=\"mouseOut(this.id)\">";
                innerHtml +="<td height=\"15px\" style=\"cursor:hand\">" +dt.Rows[i][colName]+"</td>";
                innerHtml +="</tr>";
            }
            innerHtml += "</table>\r\n";
            document.getElementById("list").innerHTML=innerHtml;
            document.getElementById("list").style.display="block";
            document.getElementById("list1").style.display="block";
       }
       else
       {
            document.getElementById("list").innerHTML="";
            document.getElementById("list").style.display="none"; 
            document.getElementById("list1").style.display="none"; 
       }
        setLocal("list");//设置层的位置
        setLocal2("list1");
       if(dt.Rows.length==1&&vsKey==dt.Rows[0][colName])
        {
            closeTiShi();
        }
   }
}
//获得当前控件的坐标(控件对象)
function getXY(e)   
{    
  x=e.offsetTop;   
  y=e.offsetLeft;   
  while(e=e.offsetParent)   
  {   
   x+=e.offsetTop;   
   y+=e.offsetLeft;   
  }   
}
//设置层的位置
function setLocal(id)
{
  var list=document.getElementById(id);
  list.style.display = "block";
  list.style.position ="absolute";
  list.style.top=x+20;   
  list.style.left=y-1; 
}
function setLocal2(id)
{
  var list=document.getElementById(id);
  list.style.display = "block";
  list.style.position ="absolute";
  list.style.top=x+173;   
  list.style.left=y+110; 
}
//隐藏层
function closeTiShi()
{
    document.getElementById("list").innerHTML="";
    document.getElementById("list").style.display="none"; 
    document.getElementById("list1").style.display="none"; 
    i = 0;
    clearTimeout(t);
}
//选中指定值到文本框(选中值,控件名)
function getCheck(name,control)
{
    document.getElementById(control).value = name;
}
//改变样式(行名称)
function mouseOver(tid)
{
    document.getElementById(tid).className="mover";
}
//改变样式(行名称)
function mouseOut(tid)
{
    document.getElementById(tid).className="mout";
}
//响应键盘事件(列名,控件名)
function keyDown(colName,control)
{  
   var vsKey = document.getElementById(control).value
   //文本框中值为空时不响应事件
   if(vsKey!=null&&vsKey!="")
   {
        //向下移动
        if(event.keyCode==40)
        {
         if(sum>0&&i<sum)
           {
            mouseOver(i);
            if(i>0)
            {
                mouseOut(i-1);
            }
            i=i+1;
           }
           document.getElementById(control).value = dt.Rows[i-1][colName];
        }
        else if(event.keyCode==38)//向上移动
        {   
            if(i>1)
            {
                if(i-2>=0)
                {
                  mouseOver(i-2);
                }
                mouseOut(i-1);
                i=i-1;
            }
            document.getElementById(control).value = dt.Rows[i-1][colName];
        }
   } 
}
